#include "StdAfx.h"

TerrainUI::TerrainUI()
{
	m_shaderUI = new ShaderUI();
	m_dialog = new Dialog(100, 100, 9, 9, "Terrain");
	char msg[255];
	sprintf_s<255>(msg, "Radius: %.02f", sSelection.GetRadius());
	m_textRadius = m_dialog->AddText(20, 35, msg);
	m_barRadius = m_dialog->AddScrollbar(140, 35, 100, this, &TerrainUI::ScrollRadius);
	m_barRadius->SetRange(10, 2000);
	SetRadius(sSelection.GetRadius());
	sprintf_s<255>(msg, "%s: %.01f", sLocale.GetLocale(2, "Intensity").c_str(), sSelection.GetIntensity());
	m_intensity = m_dialog->AddText(20, 65, msg);
	m_barIntensity = m_dialog->AddScrollbar(140, 65, 100, this, &TerrainUI::ScrollIntensity);
	m_barIntensity->SetRange(0, 200);
	SetIntensity(sSelection.GetIntensity());
	m_dialog->AddButton(this, 190, 55, "Shader", &TerrainUI::ShowShaderProc, 100);
	m_flatButton = m_dialog->AddRadioButton(20, 140, this, &TerrainUI::TerrainTypeChange);
	m_flatButton->SetText("Flat", 32);
	m_smoothButton = m_dialog->AddRadioButton(150, 140, this, &TerrainUI::TerrainTypeChange);
	m_smoothButton->SetText("Smooth", 33);
	m_linearButton = m_dialog->AddRadioButton(20, 160, this, &TerrainUI::TerrainTypeChange);
	m_linearButton->SetText("Linear");
	m_quadButton = m_dialog->AddRadioButton(150, 160, this, &TerrainUI::TerrainTypeChange);
	m_quadButton->SetText("Quadratic", 34);
	m_flatButton->Check();
	m_dialog->AddText(20, 120, "Interpolation:");

	m_heightButton = m_dialog->AddRadioButton(20, 205, this, &TerrainUI::TerrainModeChange);
	m_heightButton->SetText("Change height", 35);
	m_flattButton = m_dialog->AddRadioButton(150, 205, this, &TerrainUI::TerrainModeChange);
	m_flattButton->SetText("Flatten", 36);
	m_blurButton = m_dialog->AddRadioButton(20, 225, this, &TerrainUI::TerrainModeChange);
	m_blurButton->SetText("Blur", 37);
	m_heightButton->Check();
	m_dialog->AddText(20, 185, "Mode of change:", 38);
	m_pAlignObjs = m_dialog->AddCheckbox(this, 20, 245, "Align models to ground", &TerrainUI::AlignCheck);
	m_pAlignObjs->SetChecked(true);
	sUI.AddElement(m_dialog);
}

void TerrainUI::ScrollRadius(Scrollbar<TerrainUI>* pBar, ui32 value)
{
	float rad = value / 10.0f;
	sSelection.SetRadius(rad);
	char msg[255];
	sprintf_s<255>(msg, "Radius: %.02f", sSelection.GetRadius());
	m_textRadius->szText = msg;
}

void TerrainUI::ScrollIntensity(Scrollbar<TerrainUI>* pBar, ui32 value)
{
	float intensity = value * 0.3f;
	sSelection.SetIntensity(intensity);
	char msg[255];
	sprintf_s<255>(msg, "%s: %.01f", sLocale.GetLocale(2, "Intensity").c_str(), sSelection.GetIntensity());
	m_intensity->szText = msg;
}

void TerrainUI::TerrainTypeChange(RadioButton<TerrainUI>* pButton, bool checked)
{
	if(!checked)
	{
		pButton->Check();
		return;
	}
	if(pButton == m_flatButton && checked)
	{
		m_smoothButton->Uncheck();
		m_linearButton->Uncheck();
		m_quadButton->Uncheck();
		sSelection.SetType(0);
	}
	if(pButton == m_smoothButton && checked)
	{
		m_flatButton->Uncheck();
		m_linearButton->Uncheck();
		m_quadButton->Uncheck();
		sSelection.SetType(2);
	}
	if(pButton == m_linearButton && checked)
	{
		m_flatButton->Uncheck();
		m_smoothButton->Uncheck();
		m_quadButton->Uncheck();
		sSelection.SetType(1);
	}
	if(pButton == m_quadButton && checked)
	{
		m_flatButton->Uncheck();
		m_linearButton->Uncheck();
		m_smoothButton->Uncheck();
		sSelection.SetType(3);
	}
}

void TerrainUI::TerrainModeChange(RadioButton<TerrainUI>* pButton, bool checked)
{
	if(!checked)
	{
		pButton->Check();
		return;
	}
	if(pButton == m_heightButton)
	{
		m_flattButton->Uncheck();
		m_blurButton->Uncheck();
		sSelection.SetMode(0);
		m_quadButton->Enable();
		m_flatButton->Enable();
		ui32 val = m_barRadius->GetValue();
		m_barRadius->SetRange(10, 2000);
		m_barRadius->SetValue(val);
	}
	if(pButton == m_flattButton)
	{
		m_heightButton->Uncheck();
		m_blurButton->Uncheck();
		sSelection.SetMode(1);
		if(m_flatButton->GetChecked())
		{
			m_flatButton->Uncheck();
			m_linearButton->Check();
		}
		m_flatButton->Disable();
		if(m_quadButton->GetChecked())
		{
			m_quadButton->Uncheck();
			m_linearButton->Check();
		}
		m_quadButton->Disable();
		ui32 val = m_barRadius->GetValue();
		m_barRadius->SetRange(10, 2000);
		m_barRadius->SetValue(val);
	}
	if(pButton == m_blurButton)
	{
		m_heightButton->Uncheck();
		m_flattButton->Uncheck();
		sSelection.SetMode(2);
		if(m_flatButton->GetChecked())
		{
			m_flatButton->Uncheck();
			m_linearButton->Check();
		}
		m_flatButton->Disable();
		if(m_quadButton->GetChecked())
		{
			m_quadButton->Uncheck();
			m_linearButton->Check();
		}
		m_quadButton->Disable();
		ui32 val = m_barRadius->GetValue();
		m_barRadius->SetRange(10, 125);
		if(val > 125)
			val = 125;
		m_barRadius->SetValue(val);
	}
}